package com.seeyou.lake.ui.edt

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.widget.TextView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.seeyou.lake.R
import com.seeyou.lake.base.MyBaseActivity
import com.seeyou.lake.base.EventBusVo
import com.seeyou.lake.bean.UserBean
import com.seeyou.lake.databinding.ActivityTitleInputBinding
import com.seeyou.lake.util.GlideUtil
import com.seeyou.lake.util.SPUtils
import com.seeyou.lake.viewmodel.ModifyViewModel
import org.greenrobot.eventbus.EventBus

class TitleInputActivityMy : MyBaseActivity<ActivityTitleInputBinding, ModifyViewModel>() {

    private val bottomSheetDialog by lazy {
        BottomSheetDialog(
            this,
            R.style.RoundedBottomSheetDialog
        )
    }

    private var mSecret: Int = -1

    companion object {
        fun open(activity: Activity, filePath: String, content: String) {
            val intent = Intent(activity, TitleInputActivityMy::class.java)
            intent.putExtra("filePath", filePath)
            intent.putExtra("content", content)
            activity.startActivity(intent)
        }
    }

    override fun myViewModel(): ModifyViewModel {
        return ModifyViewModel()
    }

    override fun yourViewBing(): ActivityTitleInputBinding {
        return ActivityTitleInputBinding.inflate(layoutInflater)
    }

    override fun init() {
        val content = intent.getStringExtra("content") ?: ""
        val filePath = intent.getStringExtra("filePath") ?: ""
        val userBean = SPUtils.getInstance().getObject(SPUtils.SP_USER) as UserBean
        GlideUtil.loadRoundImageFromFile(this, filePath, 5, mViewBinding.ivPreview)
        mViewBinding.tvContent.text = content

        bottomSheetDialog.setContentView(
            LayoutInflater.from(this).inflate(R.layout.dialog_bottom_picker, null)
        )

        mViewBinding.rlPicker.setOnClickListener {
            bottomSheetDialog.show()
        }

        val openView = bottomSheetDialog.findViewById<TextView>(R.id.tv_sheet_open)
        val secretView = bottomSheetDialog.findViewById<TextView>(R.id.tv_sheet_secret)
        openView?.setOnClickListener {
            mSecret = 0
            mViewBinding.tvOpenPicker.text = getString(R.string.open)
            bottomSheetDialog.dismiss()
        }

        secretView?.setOnClickListener {
            mSecret = 1
            mViewBinding.tvOpenPicker.text = getString(R.string.secret)
            bottomSheetDialog.dismiss()
        }
        mViewBinding.tvSubmit.setOnMyClick {
            val title = mViewBinding.edtTitle.text.toString()
            if (title.isEmpty()) {
               return@setOnMyClick
            }
            if (mSecret == -1) {
               return@setOnMyClick
            }
            mViewBinding.tvSubmit.showLoading()
            mVModel.addArticle(userBean.uid, content, title, 1, mSecret, filePath)
            mViewBinding.tvSubmit.setEnable(false)
        }

        mVModel.addArticleResponse.observe(this) {
            mViewBinding.tvSubmit.hideLoading()
            if (it == "success") {
                EventBus.getDefault().post(EventBusVo(1, ""))
                EventBus.getDefault().post(EventBusVo(2, "add"))
                mVModel.toast("Submit Success!")
                finish()
            } else {
                mViewBinding.tvSubmit.setEnable(true)
            }


        }
    }

}